<template>
  <div id="main" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
    // 监听屏幕宽度变化：当浏览器发生resize事件的时候，触发echart的resize事件，重绘canvas
    window.addEventListener("resize", () => {
      this.changeWidth();
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {

    changeWidth() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ T1,T2 } = {}) {

      this.chart.setOption({
        xAxis: {
          data: [],
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          textStyle:{
                fontSize: 14,//字体大小
                color: '#82e6ff'//字体颜色
           },
          data: ['T1', 'T2'],
        },
        series: [{
          name: 'T1',

          itemStyle: {
            normal: {
              color: '#F38181',
              lineStyle: {
                color: '#F38181',
                width: 2
              },
              areaStyle: {
                color: 'rgba(243,129,129,0.09)'
              },

            }
          },
          smooth: true,
          type: 'line',
          data: T1,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: 'T2',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#95E1D3',

              lineStyle: {
                color: '#95E1D3',
                width: 2
              },
              areaStyle: {
                color: 'rgba(149,225,211,0.1)'
              }
            }
          },
          data: T2,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    },
    //    dataZoom(params){
    //     console.log(params);
		// 		let startX=params.batch[0].startValue;
		// 		let endX=params.batch[0].endValue;
    // },
  }
}
</script>

